﻿using UnityEngine;
using System;
using System.IO;
using ProtoBuf;

public class mTestOperator : MonoBehaviour
{

    void OnGUI()
    {
        if (GUILayout.Button("Test", GUILayout.Width(200f), GUILayout.Height(200f)))
        {
            mTest.SearchRequest request = new mTest.SearchRequest();
            request.page_number = 10;
            request.query = "test";
            request.result_per_page = 10;
            byte[] bytes = Serialize(request);


            mTest.SearchRequest nowRequest = (mTest.SearchRequest)Deserialize<mTest.SearchRequest>(bytes);//强转成TestMsg类型
            Debug.Log("page_number: " + nowRequest.page_number);
            Debug.Log("query: " + nowRequest.query);
            Debug.Log("result_per_page: " + nowRequest.result_per_page);
        }
    }

    public static byte[] Serialize(IExtensible msg)
    {
        byte[] result;
        using (var stream = new MemoryStream())
        {
            Serializer.Serialize(stream, msg);
            result = stream.ToArray();
        }
        return result;
    }

    public static IExtensible Deserialize<IExtensible>(byte[] message)
    {
        IExtensible result;
        using (var stream = new MemoryStream(message))
        {
            result = Serializer.Deserialize<IExtensible>(stream);
        }
        return result;
    }
}
